package com.keyidea.jfxui.core.system;

import com.keyidea.jfxui.core.system.sqlite.entity.SysUser;
import com.keyidea.jfxui.core.util.PasswordUtils;
import com.keyidea.jfxui.core.util.TokenUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
@RequiredArgsConstructor
public class LoginService {

    private final SysUserService userService;

    public boolean login(String userName, String password) {
        boolean flag = false;
        SysUser info = userService.info(userName);
        // 用户不存在
        if (Objects.isNull(info)) {
            return flag;
        }
        flag = PasswordUtils.matchesPassword(password, info.getPassword());
        if (flag) {
            TokenUtils.setCurrentUser(info);
        }
        // 匹配密码
        return flag;
    }

//    private final UserMapper userMapper;
//
//    public boolean login(String userName, String password) {
//        // 构建一个查询的wrapper, 优选LambdaQueryWrapper
//        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
//        // 构建条件，LambdaQueryWrapper可以使用get方法获取属性名
//        wrapper.eq(User::getUserName, userName);
//        User user = userMapper.selectOne(wrapper);
//        // 用户不存在
//        if (Objects.isNull(user)) {
//            return false;
//        }
//        // 匹配密码
//        return PasswordUtils.matchesPassword(password, user.getPassword());
//    }
}
